package com.neusoft.neps.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.common.domain.*;
import com.neusoft.neps.service.NepmServiceClient;
import com.neusoft.neps.service.SupervisorMesgService;
import com.neusoft.neps.service.SupervisorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supervisor")
public class SupervisorController {
    @Autowired
    private SupervisorService supervisorService;
    @Autowired
    private NepmServiceClient nepmServiceClient;
    @Autowired
    private SupervisorMesgService supervisorMesgService;

    @GetMapping("/list")
    public List<Supervisor> list(){
        return supervisorService.list();
    }

    //注册
    @PostMapping("/register")
    public Map<String,Object> register(@RequestBody Map<String,Object> supervisor){
        System.out.println(supervisor);
        Map result = supervisorService.register(supervisor);
        return result;
    }

    //查省
    @GetMapping("/queryProvince")
    public List<Province> returnProvince(){
        return nepmServiceClient.getAllProvince();
    }
    //根据省的id查city列表
    @GetMapping("/queryCity")
    public List<City> returnCity(@RequestParam(required = false) Integer id) {
        if (id == null) {
            return new ArrayList<>();
        }
        Province province = new Province();
        province.setId(nepmServiceClient.find(id).getId());
        return nepmServiceClient.query(province.getId());
    }
    //公众监督员提交信息
    @PostMapping("/addMesg")
    public int addMesg(@RequestBody SupervisorMesg supervisorMesg){
        return supervisorMesgService.addMesg(supervisorMesg);

    }
    //根据手机号查询监督员历史提交信息
    @GetMapping("/findHistory")
    public List<Map<String,Object>> queryAll(String number){
        List<Map<String,Object>> result = new ArrayList<>();
        List<SupervisorMesg> mesgList = supervisorMesgService.findAll(number);
        for (SupervisorMesg mesg:mesgList){
            Map<String,Object> map = new HashMap<>();
            map.put("address",mesg.getAddress());
            map.put("level",mesg.getLevel());
            map.put("message",mesg.getMessage());
            map.put("supervisor_phone",mesg.getSupervisor_phone());
            map.put("date",mesg.getDate());
            map.put("time",mesg.getTime());
            map.put("grider_id",mesg.getGrider_id());
            map.put("status",mesg.getStatus());
            map.put("id",mesg.getId());
            map.put("province",nepmServiceClient.find(mesg.getProvince()));
            map.put("city",nepmServiceClient.findCity(mesg.getCity()));
            result.add(map);
        }
        return result;
    }
    //公众监督员个人信息获取
    @GetMapping("/getMsg")
    public Supervisor getMsg(String phoneNumber){
        Supervisor supervisor = supervisorService.getById(phoneNumber);
        return supervisorService.getById(phoneNumber);
    }
    @GetMapping("/getchoose")
    public Map<String,Object> getchoose(Integer id){
        SupervisorMesg supervisorMesg = supervisorMesgService.getById(id);
        System.out.println(supervisorMesg);
        Map<String,Object> map =new HashMap<>();
        map.put("address",supervisorMesg.getAddress());
        map.put("level",supervisorMesg.getLevel());
        map.put("message",supervisorMesg.getMessage());
        map.put("date",supervisorMesg.getDate());
        map.put("time",supervisorMesg.getTime());
        switch (supervisorMesg.getStatus()){
            case 0:
                map.put("status","未指派");
                break;
            case 1:
                map.put("status","待检测");
                break;
            case 3:
                map.put("status","已检测");
                break;
        }
        map.put("province",nepmServiceClient.find(supervisorMesg.getProvince()).getName());
        map.put("city",nepmServiceClient.findCity(supervisorMesg.getCity()).getName());
        switch (supervisorMesg.getLevel()){
            case 1:
                map.put("level","优");
                break;
            case 2:
                map.put("level","良");
                break;
            case 3:
                map.put("level","轻度污染");
                break;
            case 4:
                map.put("level","中度污染");
                break;
            case 5:
                map.put("level","重度污染");
                break;
            case 6:
                map.put("level","严重污染");
                break;
        }
        return  map;
    }
    //获取等级信息列表
    @GetMapping("/getLevelMsg")
    public List<AQIlevel> getLevel(){
        return nepmServiceClient.getList();
    };
    //更新个人信息
    @PostMapping("/updateMesg")
    public  Boolean changeMesg(@RequestBody Supervisor supervisor){
        String md5Hex = DigestUtils.md5DigestAsHex(supervisor.getPassword().getBytes());
        supervisor.setPassword(md5Hex);
        return supervisorService.updateById(supervisor);
    }
    @GetMapping("getProvince")
    public String getProvince(int id){
        return nepmServiceClient.find(id).getName();
    }
    @GetMapping("getCity")
    public String getCity(int id){
        return nepmServiceClient.findCity(id).getName();
    }

    //其他服务调用接口
    @GetMapping("/find-belong")
    public List<SupervisorMesg> findBelong(String code){
        return supervisorMesgService.findBelong(code);
    }
    @GetMapping("find-one")
    public List<SupervisorMesg> findOne(int id){
        return supervisorMesgService.findone(id);
    }
    @GetMapping("/getBy-id")
    public Supervisor getById(String id){
        return supervisorService.getById(id);
    }
    @GetMapping("/update-status")
    public Boolean updateStatus(int id){
        return supervisorMesgService.updateStatus(id);
    }
    @PostMapping("/return-list")
    public IPage<SupervisorMesg> returnList(@RequestBody SupervisorMesgRequest request){
        return supervisorMesgService.returnList(request.getPage(),request.getConditionMap());
    }
    @GetMapping("/update-grider")
    public Boolean updateGrider(Integer bindID, String code){
        return supervisorMesgService.updateGrider(bindID,code);
    }
    @GetMapping("/get-finished")
    public List<SupervisorMesg> getFinish(){
        return supervisorMesgService.getFinish();
    }
}
